<?php

namespace app\admin\validate;

use think\Validate;

class Account extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'username' => 'require|regex:\w{3,30}|isExited:user',
        'money' => 'require|float',
    ];

    /**
     * 字段描述
     */
    protected $field = [
    ];
    /**
     * 提示消息
     */
    protected $message = [
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add' => ['username', 'money'],
    ];

    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'username' => '会员账号',
            'money' => '充值金额（元）',
        ];
        parent::__construct($rules, $message, $field);
    }

	// 自定义验证规则
    protected function isExited($value,$rule)
    {
		$this->model = model($rule);
		$user = $this->model->where('username', $value)->find();
		if(!$user)
		{
			return '会员账号不存在';
		}
		else
		{
			return true;
		}
    }
}
